﻿@using Think9.Models;
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<blockquote class="layui-elem-quote layui-text">
    数据字典指由用户定义的数据选项，可做为数据规范（如单列选择、多列选择）的动态数据源。
</blockquote>
<fieldset class="table-fieldset" id="searchfield" style="display:none">
    <legend>快速查询</legend>
    <div style="margin: 5px 5px 5px 5px">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <select id="ItemSort" name="ItemSort" lay-filter="ItemSort">
                        <option value="">==请选择==</option>
                        @foreach (SortEntity item in ((IEnumerable<SortEntity>)ViewBag.SortList))
                        {
            <option value="@item.SortID">@item.SortName</option>}
                    </select>
                </div>
                <div class="layui-inline">
                    <input type="text" name="key" id="key" placeholder="关键字" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-primary" id="search"><i class="layui-icon layui-icon-search"></i></button><button type='reset' class='layui-btn layui-btn-primary'><i class='layui-icon layui-icon-refresh'></i></button>
                </div>
            </div>
        </form>
    </div>
</fieldset>

<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;
        let $ = layui.$;

        var openWH = miniPage.getOpenWidthHeight();

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/Items/GetPageListBySearch?key=" + $("#key").val() + "&sort=" + $("#ItemSort").val(),
            limits: [10, 50, 100],
            limit: 10,
            page: true,
            method: "post",
            defaultToolbar: [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-icon-search' }, { title: '刷新显示所有数据', layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { field: "ItemSort", title: "分类", sort: true },
                { field: "EnCode", title: "字典编码", sort: true },
                { field: "FullName", title: "字典名称", sort: true },
                { field: "Remarks", title: "备注", sort: true },
                { title: "子项", width: 80, align: "center", fixed: "right", templet: "#operationTpl" },
                { title: "操作", width: 140, align: "center", fixed: "right", templet: "#operationTp2" }
            ]]
        });

        //查询
        $("#search").click(function () {
            search();
        });

        function search() {
            ThisTable.reload({
                where: { key: $("#key").val(), sort: $("#ItemSort").val() },
                page: { curr: 1 }
            });
            return false;
        }

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.EnCode);
                    break;
                case "detail":
                    detail(data.EnCode, data.FullName);
                    break;
                case "del":
                    del(data.EnCode);
                    break;
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "sort":
                    sort();
                    break;
                case "searchShow":
                    searchShow();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        function edit(id) {
            exLayer.open("编辑数据字典", "/SysTable/Items/Edit/" + id, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function detail(id, name) {
            exLayer.open(name + "-子项管理", "/SysTable/Items/ListDetail/" + id, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function del(id) {
            exLayer.confirm("将删除所选字典和该字典的所有子项，以所选字典为数据源的数据规范将不再正常显示，确定要删除吗？", function () {
                exUtils.ajax("/SysTable/Items/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function searchShow() {
            var display = $('#searchfield').css('display');
            if (display == 'none') {
                $("#searchfield").show();
            }
            else {
                $("#searchfield").hide();
            }
        }

        function add() {
            exLayer.open("添加数据字典", "/SysTable/Items/Add", openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function sort() {
            exLayer.open("数据字典分类", "/SysBasic/Sort/List?classid=CAT_dict", openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
        }

        function refresh() {
            ThisTable.reload({
                where: { key: '', sort: '' }
            });
        }
    })
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class='layui-btn layui-btn-sm layui-btn-normal ' lay-event='add' id='add'><i class="fa fa-plus"></i>添加字典</button>
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="sort" id="sort"><i class="fa fa-paste"></i>分类管理</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class='layui-btn layui-btn-normal layui-btn-xs' href='javascript:;' lay-event='detail' id='detail'><i class="fa fa-list-ol"></i> {{ d.Amount }}</a>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTp2">
    <a class='layui-btn layui-btn-normal layui-btn-xs ' href='javascript:;' lay-event='edit' id='edit'>编辑</a>
    <a class='layui-btn layui-btn-xs layui-btn-danger' href='javascript:;' lay-event='del' id='del'>删除</a>
</script>